package com.sjn.common.response;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

/**
 * @author EDZ
 */
@Schema(description = "")
@Data
public class ResponseResult<T> {
    /**
     * 返回信息
     */
    @Schema(description = "返回信息")
    private Meta meta;
    /**
     * 数据信息
     */
    @Schema(description = "数据信息")
    private T data;
    /**
     * 分页信息
     */
    @Schema(description = "分页信息")
    private Page page;

    public T getData() {
        return data;
    }

    public Page getPage() {
        return page;
    }

    public ResponseResult() {
        success();
    }

    public ResponseResult(T data, Page page) {
        this.data = data;
        this.page = page;
    }

    public ResponseResult failure(ResponseCode code) {
        this.meta = new Meta(code);
        return this;
    }

    public ResponseResult failure() {
        this.meta = new Meta(ResponseCode.FAILURE);
        return this;
    }

    public ResponseResult failure(T data) {
        this.meta = new Meta(-1, (String) data);
        return this;
    }

    public ResponseResult failure(Integer code, String msg) {
        this.meta = new Meta(code, msg);
        return this;
    }

    public ResponseResult success() {
        this.meta = new Meta(ResponseCode.SUCCESS);
        return this;
    }

    public ResponseResult success(T data) {
        this.meta = new Meta(ResponseCode.SUCCESS);
        this.data = data;
        return this;
    }

    public ResponseResult success(ResponseCode code) {
        this.meta = new Meta(code);
        this.data = null;
        return this;
    }

    public ResponseResult success(ResponseCode code, T data) {
        this.meta = new Meta(code);
        this.data = data;
        return this;
    }

    public ResponseResult success(T data, Page page) {
        this.meta = new Meta(ResponseCode.SUCCESS);
        this.data = data;
        this.page = page;
        return this;
    }

    public ResponseResult success(Page page, T data) {
        this.meta = new Meta(ResponseCode.SUCCESS);
        this.data = data;
        this.page = page;
        return this;
    }

    public ResponseResult success(PageData pageData) {
        this.meta = new Meta(ResponseCode.SUCCESS);
        this.data = (T) pageData.getData();
        this.page = pageData.getPage();
        return this;
    }

    public ResponseResult success(ResponseCode code, T data, Page page) {
        this.meta = new Meta(code);
        this.data = data;
        this.page = page;
        return this;
    }

    @Override
    public String toString() {
        return "ResponseResult{" +
                "meta=" + meta +
                ", data=" + data +
                ", page=" + page +
                '}';
    }

    public Meta getMeta() {
        return meta;
    }

    public void setMeta(Meta meta) {
        this.meta = meta;
    }

    public void setData(T data) {
        this.data = data;
    }

    public void setPage(Page page) {
        this.page = page;
    }
}
